package com.punai.dataclean.domain.vo;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.multi.ListValueMap;
import com.punai.common.core.domain.CommonConstants;
import com.punai.common.core.domain.TreeParentBo;
import com.punai.config.domain.ConfigInventoryChapter;

import java.math.BigDecimal;
import java.util.*;

public class IncomeDataCleanVo {
    private Long companyId;

    private String inventoryNorm;

    private String groupId;

    private String id;

    private String pid;

    private String type;

    private String code;

    private String name;

    private List<IncomeDataCleanVo> children = new ArrayList<>();


    private String inventoryCode;
    private String inventoryName;
    private String inventoryUnit;
    private String inventorySpec;
    private String inventoryNewSpec;
    private String inventoryContent;
    private String inventoryCalcrule;
    private String cleanStatus;
    private String cleanDate;
    private String countSl;

    private String gxCbType;
    private String categoryId;
    private String versionId;

    private String dataJson;
    private String oinventoryCode;
    private String oinventoryName;

    private BigDecimal inventoryPrice;
    private String proName;
    private String priceType;
    private String cleanDateStart;
    private String cleanDateEnd;
    private String address;
    private String companyOfDep;

    private Map<String, List<String>> selKeyLinkids = new HashMap<>();
    private List<String> selKeys = new ArrayList<>();
    private String newKey;


    public static List<IncomeDataCleanVo> convert(List<ConfigInventoryChapter> list) {
        List<IncomeDataCleanVo> res = new ArrayList<>();
        ListValueMap<String, TreeParentBo> levelMap = new ListValueMap<>();
        for (TreeParentBo treeParentBo : list) {
            String pid = treeParentBo.getPid();
            levelMap.putValue(pid, treeParentBo);
        }
        List<TreeParentBo> roots = levelMap.get(CommonConstants.DEF_PID);
        if (CollUtil.isNotEmpty(roots)) {
            roots.sort(Comparator.comparing(TreeParentBo::getOrderNum));
            for (TreeParentBo root : roots) {
                IncomeDataCleanVo bo = new IncomeDataCleanVo();
                bo.setId(root.getId());
                bo.setPid(root.getPid());
                bo.setName(root.getName());
                bo.setCode(root.getCode());
                bo.setType(root.getType());
                res.add(bo);
                getTreeChildren(bo, levelMap);
            }
        }
        return res;
    }

    private static void getTreeChildren(IncomeDataCleanVo bo, ListValueMap<String, TreeParentBo> levelMap) {
        String id = bo.getId();
        List<TreeParentBo> chapters = levelMap.get(id);
        if (CollUtil.isNotEmpty(chapters)) {
            chapters.sort(Comparator.comparing(TreeParentBo::getOrderNum));
            for (TreeParentBo root : chapters) {
                IncomeDataCleanVo cbo = new IncomeDataCleanVo();
                cbo.setId(root.getId());
                cbo.setPid(root.getPid());
                cbo.setName(root.getName());
                cbo.setCode(root.getCode());
                cbo.setType(root.getType());
                bo.getChildren().add(cbo);
                getTreeChildren(cbo, levelMap);
            }
        }
    }

    public Long getCompanyId() {
        return companyId;
    }

    public void setCompanyId(Long companyId) {
        this.companyId = companyId;
    }

    public String getInventoryNorm() {
        return inventoryNorm;
    }

    public void setInventoryNorm(String inventoryNorm) {
        this.inventoryNorm = inventoryNorm;
    }

    public String getGroupId() {
        return groupId;
    }

    public void setGroupId(String groupId) {
        this.groupId = groupId;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getPid() {
        return pid;
    }

    public void setPid(String pid) {
        this.pid = pid;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<IncomeDataCleanVo> getChildren() {
        return children;
    }

    public void setChildren(List<IncomeDataCleanVo> children) {
        this.children = children;
    }

    public String getInventoryCode() {
        return inventoryCode;
    }

    public void setInventoryCode(String inventoryCode) {
        this.inventoryCode = inventoryCode;
    }

    public String getInventoryName() {
        return inventoryName;
    }

    public void setInventoryName(String inventoryName) {
        this.inventoryName = inventoryName;
    }

    public String getInventoryUnit() {
        return inventoryUnit;
    }

    public void setInventoryUnit(String inventoryUnit) {
        this.inventoryUnit = inventoryUnit;
    }

    public String getInventorySpec() {
        return inventorySpec;
    }

    public void setInventorySpec(String inventorySpec) {
        this.inventorySpec = inventorySpec;
    }

    public String getInventoryNewSpec() {
        return inventoryNewSpec;
    }

    public void setInventoryNewSpec(String inventoryNewSpec) {
        this.inventoryNewSpec = inventoryNewSpec;
    }

    public String getInventoryContent() {
        return inventoryContent;
    }

    public void setInventoryContent(String inventoryContent) {
        this.inventoryContent = inventoryContent;
    }

    public String getInventoryCalcrule() {
        return inventoryCalcrule;
    }

    public void setInventoryCalcrule(String inventoryCalcrule) {
        this.inventoryCalcrule = inventoryCalcrule;
    }

    public String getCleanStatus() {
        return cleanStatus;
    }

    public void setCleanStatus(String cleanStatus) {
        this.cleanStatus = cleanStatus;
    }

    public String getCleanDate() {
        return cleanDate;
    }

    public void setCleanDate(String cleanDate) {
        this.cleanDate = cleanDate;
    }

    public String getCountSl() {
        return countSl;
    }

    public void setCountSl(String countSl) {
        this.countSl = countSl;
    }

    public String getGxCbType() {
        return gxCbType;
    }

    public void setGxCbType(String gxCbType) {
        this.gxCbType = gxCbType;
    }

    public String getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(String categoryId) {
        this.categoryId = categoryId;
    }

    public String getVersionId() {
        return versionId;
    }

    public void setVersionId(String versionId) {
        this.versionId = versionId;
    }

    public BigDecimal getInventoryPrice() {
        return inventoryPrice;
    }

    public void setInventoryPrice(BigDecimal inventoryPrice) {
        this.inventoryPrice = inventoryPrice;
    }

    public String getProName() {
        return proName;
    }

    public void setProName(String proName) {
        this.proName = proName;
    }

    public String getPriceType() {
        return priceType;
    }

    public void setPriceType(String priceType) {
        this.priceType = priceType;
    }

    public String getCleanDateStart() {
        return cleanDateStart;
    }

    public void setCleanDateStart(String cleanDateStart) {
        this.cleanDateStart = cleanDateStart;
    }

    public String getCleanDateEnd() {
        return cleanDateEnd;
    }

    public void setCleanDateEnd(String cleanDateEnd) {
        this.cleanDateEnd = cleanDateEnd;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getCompanyOfDep() {
        return companyOfDep;
    }

    public void setCompanyOfDep(String companyOfDep) {
        this.companyOfDep = companyOfDep;
    }

    public Map<String, List<String>> getSelKeyLinkids() {
        return selKeyLinkids;
    }

    public void setSelKeyLinkids(Map<String, List<String>> selKeyLinkids) {
        this.selKeyLinkids = selKeyLinkids;
    }

    public List<String> getSelKeys() {
        return selKeys;
    }

    public void setSelKeys(List<String> selKeys) {
        this.selKeys = selKeys;
    }

    public String getNewKey() {
        return newKey;
    }

    public void setNewKey(String newKey) {
        this.newKey = newKey;
    }

    public String getDataJson() {
        return dataJson;
    }

    public void setDataJson(String dataJson) {
        this.dataJson = dataJson;
    }

    public String getOinventoryCode() {
        return oinventoryCode;
    }

    public void setOinventoryCode(String oinventoryCode) {
        this.oinventoryCode = oinventoryCode;
    }

    public String getOinventoryName() {
        return oinventoryName;
    }

    public void setOinventoryName(String oinventoryName) {
        this.oinventoryName = oinventoryName;
    }
}
